package com.so206.controller.payment;

import com.github.pagehelper.PageInfo;
import com.so206.po.*;
import com.so206.security.AESOperator;
import com.so206.service.*;
import com.so206.utils.Base64;
import com.so206.utils.MailUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付配置
 */
@Controller
@RequestMapping("/admin/payconfig")
public class PayConfigController {

    @Autowired
    private PayConfigService payConfigService;

    @Autowired
    private MailUtils utils;

    @Autowired
    private AlipayConfigService alipayConfigService;

    @Autowired
    private EPayConfigService ePayConfigService;

    @Autowired
    private CodePayConfigService codePayConfigService;

    @Autowired
    private QQPayConfigService qqPayConfigService;

    @Autowired
    private WxPayConfigService wxPayConfigService;

    /**
     * 获取配置
     */
    @RequiresPermissions("pay_config")
    @RequestMapping("/edit")
    public String edit(Model model) {
        SystemPayConfig config = payConfigService.getPayConfig(1L);
        model.addAttribute("model", config);
        List<AlipayConfig> alipayConfigs = alipayConfigService.find_all_conn(1);
        List<EpayConfig> epayConfigs = ePayConfigService.find_all_conn(1);
        List<CodepayConfig> codepayConfigs = codePayConfigService.find_all_conn(1);
        List<QqpayConfig> qqpayConfigs = qqPayConfigService.find_all_conn(1);
        List<WxpayConfig> wxpayConfigs = wxPayConfigService.find_all_conn(1);
        model.addAttribute("alipayConfigs", alipayConfigs);
        model.addAttribute("epayConfigs", epayConfigs);
        model.addAttribute("codepayConfigs", codepayConfigs);
        model.addAttribute("qqpayConfigs", qqpayConfigs);
        model.addAttribute("wxpayConfigs", wxpayConfigs);
        return "admin/pay_config/edit";
    }

    /**
     * 更新配置
     */
    @RequiresPermissions("pay_config")
    @RequestMapping("/update")
    @ResponseBody
    public Map update(SystemPayConfig config) {
        HashMap<Object, Object> map = new HashMap<>();
        payConfigService.updatePPayConfig(config);
        map.put("msg", "更新成功!");
        map.put("code", "0");
        return map;
    }

    /**
     * 易支付通道分页
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_list")
    public String page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer rows,
                       @RequestParam(defaultValue = "false") Boolean ajax,
                       Model model, Integer status, Integer payType, String name, String url) {
        PageInfo<EpayConfig> bean = ePayConfigService.find_by_page(page, rows, status, payType, name, url);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("payType", payType);
        model.addAttribute("name", name);
        model.addAttribute("url", url);
        if (ajax) {
            return "admin/pay_config/epay/list::pageNode";
        } else {
            return "admin/pay_config/epay/list";
        }
    }

    /**
     * 易支付通道 -- 添加
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_add")
    public String epay_add() {
        return "admin/pay_config/epay/add";
    }

    /**
     * 易支付通道 -- 保存
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_save")
    @ResponseBody
    public Map epay_save(EpayConfig config) {
        ePayConfigService.save(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "添加成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 易支付通道 -- 删除
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_delete")
    @ResponseBody
    public Map epay_delete(Long id) {
        ePayConfigService.delete_by_id(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 易支付通道 -- 查看
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_view")
    public String epay_view(Model model, Long id) {
        EpayConfig configService_by_id = ePayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/epay/view";
    }

    /**
     * 易支付通道 -- 编辑
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_edit")
    public String epay_edit(Model model, Long id) {
        EpayConfig configService_by_id = ePayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/epay/edit";
    }

    /**
     * 易支付通道 -- 删除
     */
    @RequiresPermissions("epay_config")
    @RequestMapping("/epay_update")
    @ResponseBody
    public Map epay_update(EpayConfig config) {
        ePayConfigService.update(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }


    /**
     * 码支付通道 -- 分页
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_list")
    public String codepay_page(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "20") Integer rows,
                               @RequestParam(defaultValue = "false") Boolean ajax,
                               Model model, Integer status, Integer payType, String name) {
        PageInfo<CodepayConfig> bean = codePayConfigService.find_by_page(page, rows, status, payType, name);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("payType", payType);
        model.addAttribute("name", name);
        if (ajax) {
            return "admin/pay_config/codepay/list::pageNode";
        } else {
            return "admin/pay_config/codepay/list";
        }
    }

    /**
     * 码支付通道 -- 添加
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_add")
    public String codepay_add() {
        return "admin/pay_config/codepay/add";
    }

    /**
     * 码支付通道 -- 保存
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_save")
    @ResponseBody
    public Map codepay_save(CodepayConfig config) {
        codePayConfigService.save(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "添加成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 码支付通道 -- 删除
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_delete")
    @ResponseBody
    public Map codepay_delete(Long id) {
        codePayConfigService.delete_by_id(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 码支付通道 -- 查看
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_view")
    public String codepay_view(Model model, Long id) {
        CodepayConfig configService_by_id = codePayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/codepay/view";
    }

    /**
     * 码支付通道 -- 编辑
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_edit")
    public String codepay_edit(Model model, Long id) {
        CodepayConfig configService_by_id = codePayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/codepay/edit";
    }

    /**
     * 码支付通道 -- 删除
     */
    @RequiresPermissions("code_pay_config")
    @RequestMapping("/codepay_update")
    @ResponseBody
    public Map codepay_update(CodepayConfig config) {
        codePayConfigService.update(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }


    /**
     * 支付宝通道 -- 分页
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_list")
    public String alipay_page(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer rows,
                              @RequestParam(defaultValue = "false") Boolean ajax,
                              Model model, Integer status, String name, String mchid, String appid) {
        PageInfo<AlipayConfig> bean = alipayConfigService.find_by_page(page, rows, status, name, mchid, appid);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("name", name);
        model.addAttribute("mchid", mchid);
        model.addAttribute("appid", appid);
        if (ajax) {
            return "admin/pay_config/alipay/list::pageNode";
        } else {
            return "admin/pay_config/alipay/list";
        }
    }

    /**
     * 支付宝通道 -- 添加
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_add")
    public String alipay_add() {
        return "admin/pay_config/alipay/add";
    }

    /**
     * 支付宝通道 -- 保存
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_save")
    @ResponseBody
    public Map alipay_save(AlipayConfig config) {
        alipayConfigService.save(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "添加成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 支付宝通道 -- 删除
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_delete")
    @ResponseBody
    public Map alipay_delete(Long id) {
        alipayConfigService.delete_by_id(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 支付宝通道 -- 查看
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_view")
    public String alipay_view(Model model, Long id) {
        AlipayConfig configService_by_id = alipayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/alipay/view";
    }

    /**
     * 支付宝通道 -- 编辑
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_edit")
    public String alipay_edit(Model model, Long id) {
        AlipayConfig configService_by_id = alipayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/alipay/edit";
    }

    /**
     * 支付宝通道 -- 删除
     */
    @RequiresPermissions("alipay_config")
    @RequestMapping("/alipay_update")
    @ResponseBody
    public Map alipay_update(AlipayConfig config) {
        alipayConfigService.update(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 微信通道 -- 分页
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_list")
    public String wxpay_page(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer rows,
                             @RequestParam(defaultValue = "false") Boolean ajax,
                             Model model, Integer status, String name, String mchid, String appid) {
        PageInfo<WxpayConfig> bean = wxPayConfigService.find_by_page(page, rows, status, name, mchid, appid);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("name", name);
        model.addAttribute("mchid", mchid);
        model.addAttribute("appid", appid);
        if (ajax) {
            return "admin/pay_config/wxpay/list::pageNode";
        } else {
            return "admin/pay_config/wxpay/list";
        }
    }

    /**
     * 微信通道 -- 添加
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_add")
    public String wxpay_add() {
        return "admin/pay_config/wxpay/add";
    }

    /**
     * 微信通道 -- 保存
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_save")
    @ResponseBody
    public Map wxpay_save(WxpayConfig config, String file_upload) {
        if (file_upload != null) {
            int start = file_upload.indexOf("base64,") + "base64,".length();
            String s = file_upload.substring(start);
            byte[] bytes = Base64.decode(s);
            config.setWxCert(bytes);
        } else {
            config.setWxCert(null);
        }
        wxPayConfigService.save(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "添加成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 微信通道 -- 删除
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_delete")
    @ResponseBody
    public Map wxpay_delete(Long id) {
        wxPayConfigService.delete_by_id(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 微信通道 -- 查看
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_view")
    public String wxpay_view(Model model, Long id) {
        WxpayConfig configService_by_id = wxPayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        byte[] cert = configService_by_id.getWxCert();
        if (cert != null) {
            String str = AESOperator.parseByte2HexStr(cert);
            model.addAttribute("cert", str);
        }
        return "admin/pay_config/wxpay/view";
    }

    /**
     * 微信通道 -- 编辑
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_edit")
    public String wxpay_edit(Model model, Long id) {
        WxpayConfig configService_by_id = wxPayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/wxpay/edit";
    }

    /**
     * 微信通道 -- 删除
     */
    @RequiresPermissions("wxpay_config")
    @RequestMapping("/wxpay_update")
    @ResponseBody
    public Map wxpay_update(WxpayConfig config, String file_upload) {
        if (file_upload != null) {
            int start = file_upload.indexOf("base64,") + "base64,".length();
            String s = file_upload.substring(start);
            byte[] bytes = Base64.decode(s);
            config.setWxCert(bytes);
        } else {
            config.setWxCert(null);
        }
        wxPayConfigService.update(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * QQ钱包通道 -- 分页
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_list")
    public String qqpay_page(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer rows,
                             @RequestParam(defaultValue = "false") Boolean ajax,
                             Model model, Integer status, String name, String mchid) {
        PageInfo<QqpayConfig> bean = qqPayConfigService.find_by_page(page, rows, status, name, mchid);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("name", name);
        model.addAttribute("mchid", mchid);
        if (ajax) {
            return "admin/pay_config/qqpay/list::pageNode";
        } else {
            return "admin/pay_config/qqpay/list";
        }
    }

    /**
     * QQ钱包通道 -- 添加
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_add")
    public String qqpay_add() {
        return "admin/pay_config/qqpay/add";
    }

    /**
     * QQ钱包通道 -- 保存
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_save")
    @ResponseBody
    public Map qqpay_save(QqpayConfig config, String file_upload) {
        if (file_upload != null) {
            int start = file_upload.indexOf("base64,") + "base64,".length();
            String s = file_upload.substring(start);
            byte[] bytes = Base64.decode(s);
            config.setPayCert(bytes);
        } else {
            config.setPayCert(null);
        }
        qqPayConfigService.save(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "添加成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * QQ钱包通道 -- 删除
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_delete")
    @ResponseBody
    public Map qqpay_delete(Long id) {
        qqPayConfigService.delete_by_id(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * QQ钱包通道 -- 查看
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_view")
    public String qqpay_view(Model model, Long id) {
        QqpayConfig configService_by_id = qqPayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        byte[] cert = configService_by_id.getPayCert();
        if (cert != null) {
            String str = AESOperator.parseByte2HexStr(cert);
            model.addAttribute("cert", str);
        }
        return "admin/pay_config/qqpay/view";
    }

    /**
     * QQ钱包通道 -- 编辑
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_edit")
    public String qqpay_edit(Model model, Long id) {
        QqpayConfig configService_by_id = qqPayConfigService.find_by_id(id);
        model.addAttribute("model", configService_by_id);
        return "admin/pay_config/qqpay/edit";
    }

    /**
     * QQ钱包通道 -- 删除
     */
    @RequiresPermissions("qq_pay_config")
    @RequestMapping("/qqpay_update")
    @ResponseBody
    public Map qqpay_update(QqpayConfig config, String file_upload) {
        if (file_upload != null) {
            int start = file_upload.indexOf("base64,") + "base64,".length();
            String s = file_upload.substring(start);
            byte[] bytes = Base64.decode(s);
            config.setPayCert(bytes);
        } else {
            config.setPayCert(null);
        }
        qqPayConfigService.update(config);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }
}
